package com.springcloudalibaba.bclass.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @描述:
 * @作者: lixing
 * @日期 2021/6/9 8:59
 */
public class DemoUtil {
    public static void main(String[] args) throws IOException {
//        Random random = new java.util.Random();
//        // 返回0 to x的一个随机数但不会取到x，即返回[0,x)闭开区间的值。
//        for (int i = 0; i < 100; i++) {
//            int rn = random.nextInt(100);
//            System.out.println(rn);
//        }

//        String url = "2021/06/21/74a7402410de44db875b0bda993d5fbb.mp4";
//        url = url.replace(url.substring(url.indexOf(".")+1), "png");
//        System.out.println(url);

//        int random = new Random().nextInt(10);
//        System.out.println(random);

//        for (int i = 0 ; i < 10; i++) {
//            int j =  new Random().nextInt(10 - 5) + 5 + 1;
//            System.out.println(j);
//        }

//        int size = 2; // 0,1
//        for (int i = 0 ; i < 100; i++) {
//            int random = new Random().nextInt(size);
//            if (random > 1) {
//                System.out.println(random);
//            }
//        }

//        String item = "https://etiger.oss-cn-hangzhou.aliyuncs.com/cbe26bdae02f4bf6ba4a066cea3cbe66.pdf";
//        item = item.replace(item = "https://etiger.oss-cn-hangzhou.aliyuncs.com", "").substring(1);
//        System.out.println(item);

//        List<String> keys = new ArrayList<>();
//        keys.add("2021/07/30/8407077544754f86b919dfb53fe75346.pdf");
//        keys.add("2021/07/30/3d09ae018d38401cb91155f33d274f65.png");
//        keys = keys.stream().map(item -> {
//            item = item.replace("2021", "").substring(1);
//            return item;
//        }).collect(Collectors.toList());
//        System.out.println(JSONObject.toJSONString(keys));

//        String nickName = "18013896707";
//        nickName = nickName.replace(nickName.substring(3,7), "****");
//        System.out.println(nickName);

//        String s = "[\"http://192.168.1.243/2021/08/18/35cad8ffa14742f3bf6896d743e96817.png\",\"http://192.168.1.243/2021/08/18/580c174560eb404baf2165c314a89395.png\"]";
//        JSONArray jsonArray = JSONArray.parseArray(s);
//        System.out.println(JSONObject.toJSONString(jsonArray));


//        String imageUrl = "D:\\测试图片.png";
//        imageUrl = "https://20210614-test.oss-cn-hangzhou.aliyuncs.com/%E6%B5%8B%E8%AF%95%E5%9B%BE%E7%89%87.png";
//
//        String pdfUrl = "D:\\测试pdf.pdf";
//        pdfUrl = "https://20210614-test.oss-cn-hangzhou.aliyuncs.com/%E6%B5%8B%E8%AF%95pdf.pdf";
//
//        String imageBase64 = Base64Util.fileToBase64(imageUrl);
//        System.out.println("imageBase64-"+ imageBase64.length());
//
//        String pdfBase64 = Base64Util.fileToBase64(pdfUrl);
//        System.out.println("pdfBase64-"+ pdfBase64.length());
//
//        Base64Util.base64ToFile(imageBase64, "png", "D:\\");
//        Base64Util.base64ToFile(pdfBase64, "pdf", "D:\\");

//        int total = 11;//数据总量
//        int row = 3;//一页显示条数
//        int totalPages;//总页数
//        totalPages = total / row;
//        if (total % row != 0){
//            totalPages ++;
//        }
//        System.out.println(totalPages);//此方法容易理解
//        System.out.println( (total-1)/row + 1 );//此方法使用较多
//        System.out.println((total+row-1)/row);


//        int result = (count-1)/ip + 1;
//        System.out.println( result );
//        System.out.println("---");


//        int count = 60;
//        int ip = count/2 == 0 ? 1 : count/2;
//        System.out.println(ip);
//        System.out.println("");
//        for (int i = 0; i < count; i++) {
//            System.out.println(i%ip);
//        }

        // 过滤出对象中指定的字段到List中
        DemoVo demoVo1 = new DemoVo(310111, 31011101, 51.00, 50.00);
        DemoVo demoVo2 = new DemoVo(310111, 31011102, 91.00, 50.00);
        DemoVo demoVo3 = new DemoVo(310112, 31011201, 53.00, 50.00);
        DemoVo demoVo4 = new DemoVo(310112, 31011202, 93.00, 50.00);

        List<DemoVo> demoVoList = new ArrayList<>();
        demoVoList.add(demoVo1);
        demoVoList.add(demoVo2);
        demoVoList.add(demoVo3);
        demoVoList.add(demoVo4);

        // 获取班号
        List<Integer> classNumList = demoVoList.stream().map(DemoVo::getClassNum).distinct().collect(Collectors.toList());
        Set<Integer> classNumSet = demoVoList.stream().map(DemoVo::getClassNum).collect(Collectors.toSet());
        // 聚合操作
        // 语文成绩的最高分
        Double ywScoreMax = demoVoList.stream().map(DemoVo::getYwScore).max(Double::compare).get();
        // 语文成绩的最低分
        Double ywScoreMin = demoVoList.stream().map(DemoVo::getYwScore).min(Double::compare).get();
        // 语文成绩的平均分
        Double avgYwScore = demoVoList.stream().collect(Collectors.averagingDouble(DemoVo::getYwScore));
        // 语文成绩的总分
        Double allYwScore = demoVoList.stream().mapToDouble(DemoVo::getYwScore).sum();
        // 数据总数 + 求和 + 最小值 + 最大值
        DoubleSummaryStatistics statistics = demoVoList.stream().collect(Collectors.summarizingDouble(DemoVo::getYwScore));

        // 按照班级分组
        Map<Integer, List<DemoVo>> demoVoListByClassNum = demoVoList.stream().collect(Collectors.groupingBy(DemoVo::getClassNum));

        // 分区（语文成绩及格）
        Map<Boolean, List<DemoVo>> demoVoListByJg = demoVoList.stream().collect(Collectors.partitioningBy(v -> v.getYwScore() > 60));
    }
}

@Data
class DemoVo {
    /** 班号 */
    private Integer classNum;
    /** 学号 */
    private Integer stuNum;
    /** 语文得分 */
    private Double ywScore;
    /** 数学得分 */
    private Double sxScore;

    public DemoVo(Integer classNum, Integer stuNum, Double ywScore, Double sxScore) {
        this.classNum = classNum;
        this.stuNum = stuNum;
        this.ywScore = ywScore;
        this.sxScore = sxScore;
    }
}